package controllers;

import models.Login;
import play.data.Form;
import play.mvc.*;
import views.html.*;

public class Application extends Controller {
          
	        //Form do login
	        private static Form<Login> formLogin = new Form<Login>(Login.class);
	        
	        public static Result index(){
                return ok(index.render(""));
	        }
	        
	        //Pagina de Login
	        public static Result login(){
	                return ok(views.html.login.render(formLogin));
	        }
	        
	        //Metodo de autenticação de usuario
	        public static Result autenticar(){
	        	Form<Login> f = formLogin.bindFromRequest();
	        	
                if (f.hasErrors()) {
                        return badRequest(login.render(f));
                } else {
                        session("login", f.get().login);
                        //session("administrador", f.get().isAdministrador());
                        
                        if(f.get().login.equalsIgnoreCase("administrador")){
                        	return redirect(routes.Application.inicioAdministrador());
                        }else{
                        	return redirect(routes.Application.inicio());
                        }
                }
	        }
	        
	        //Pagina Inicial
	       @Security.Authenticated(Secured.class)
	       public static Result inicio() {
	    	   	//session().clear();
                return ok(inicio.render());
	        }
	       
	       //Pagina Inicial Administrador
	       @Security.Authenticated(SecuredAdministrador.class)
	       public static Result inicioAdministrador() {
	    	   	//session().clear();
                return ok(inicioAdministrador.render());
	        }
	        
	        //Metodo de logout do sistema
	        @Security.Authenticated(Secured.class)
	        public static Result logout(){
	                session().clear();
	                session().clear();
	        return redirect(routes.Application.login());
	                
	        }
}
